package cn.cxion.chat.app.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.cxion.chat.R;
import cn.cxion.chat.app.activity.ConstantActivity;
import cn.cxion.chat.app.adapter.ApprovalProcessAdapter;
import cn.cxion.chat.app.adapter.WrapRecyclerAdapter;
import cn.cxion.chat.app.bean.CheckContractBean;
import cn.cxion.chat.app.bean.MessageAction;
import cn.cxion.chat.app.bean.MessageEvent;
import cn.cxion.chat.app.constant.Constant;
import cn.cxion.chat.app.inter.OnSaveListener;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.view.CheckContentView;
import cn.cxion.chat.app.view.WrapRecyclerView;

public class ClaimFragment extends Fragment {



    WrapRecyclerView wrapRecyclerView;
    @Bind(R.id.ll_hold)
    LinearLayout llHold;
    private CheckContractBean checkContractBean;
    private ApprovalProcessAdapter approvalProcessAdapter;
    private   WrapRecyclerAdapter wrapRecyclerAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.linear_layout, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        init();
        return view;
    }

    public CheckContractBean getCheckContractBean() {
        return checkContractBean;
    }

    public void setCheckContractBean(CheckContractBean checkContractBean) {
        this.checkContractBean = checkContractBean;
    }

    private void init() {
        llHold.addView(new CheckContentView(getContext()).getView());
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        llHold.addView(LayoutInflater.from(getContext()).inflate(R.layout.divide_layout,null),params);
        wrapRecyclerView= (WrapRecyclerView) LayoutInflater.from(getContext()).inflate(R.layout.recycle_layout,null);
        wrapRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        approvalProcessAdapter=new ApprovalProcessAdapter(getContext(),Constant.CLAIM,checkContractBean);
         wrapRecyclerAdapter = new WrapRecyclerAdapter(approvalProcessAdapter);
        wrapRecyclerView.setAdapter(wrapRecyclerAdapter);
        LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        llHold.addView(wrapRecyclerView,params2);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(MessageEvent event){
        LogUtil.print("刷新");
        if(event.getKey()== MessageAction.RefreshCliam){
            for (CheckContractBean bean: Constant.getContractBeanList()){
                if(bean.getUuid().equals(checkContractBean.getUuid())){
                    checkContractBean=bean;
                }
            }
            LogUtil.print("checkContractBean  "+checkContractBean.getCheckProcessDetailList().size());
            approvalProcessAdapter.setCheckContractBean(checkContractBean);
            approvalProcessAdapter.notifyDataSetChanged();
            wrapRecyclerAdapter.notifyDataSetChanged();
        }
    }
    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
